package 强化训练;

import java.util.ArrayList;

public class day15手套 {
    public static void main(String[] args) {
        int[] left = {0,7,1,6,1};
        int[] right = {1,5,0,6,0};
        System.out.println(findMinimum(5, left, right));
    }

    public static int findMinimum(int n, int[] left, int[] right) {
        // write code here
        ArrayList<Integer> arrayList = new ArrayList<>();//保存left和right中同时不为0的下标
        for (int i = 0; i < n; i++) {
            if (left[i] != 0 && right[i] != 0){
                arrayList.add(i);
            }
        }
        //计算左手中不能组合的手套的个数，当这个个数在+1就可以和右手中的手套进行组合，至少出现一双时可以组合的
        int leftCount = 1;//左手的个数
        for (int i = 0; i < n; i++) {
            if (!arrayList.contains(i)){
                leftCount += left[i];
            }
        }
        //计算右手不能组合的手套个数，初始为1
        int rightCount = 1;
        for (int i = 0; i < n; i++) {
            if (!arrayList.contains(i)){
                rightCount += right[i];
            }
        }
        //将可以组合的右手手套保存在一个数组中
        int[] arr = new int[arrayList.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = right[arrayList.get(i)];
        }
        //找到arr中最小的那个数，记录其下标
        int index = 0;
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (min > arr[i]){
                min = arr[i];
                index = i;
            }
        }
        //不加最小值
        for (int i = 0; i < arr.length; i++) {
            if (!(i == index)){
                rightCount += arr[i];
            }
        }
        return leftCount + rightCount;
    }
}
